/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelLogin.java
 *
 * Created on Dec 3, 2010, 10:39:16 PM
 */
package edu.unindra.company.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.GroupLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JPanel;

import edu.unindra.company.dao.entity.Empl;
import edu.unindra.company.dao.impl.EmployeeImpl;
import edu.unindra.company.template.ButtonTransparan;
import usu.widget.ButtonGlass;
import usu.widget.glass.TextBoxGlass;

import javax.swing.ImageIcon;

import edu.unindra.company.gui.ReportReview;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;

public class FormMainIDCard extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8349051460419454260L;

	private ButtonGlass btnPrint;
	private ButtonTransparan buttonTransparan1;

	private TextBoxGlass txtName;
	private TextBoxGlass txtNik;
	private TextBoxGlass txtNim;
	private TextBoxGlass txtJabatan;

	private JLabel lblNama;
	private JLabel lblNik;
	private JLabel lblJabatan;
	private JLabel lblTitle;

	private EmployeeImpl empl = null;
	private Empl em = null;

	private JasperReport jasperReport;
	private JasperDesign jasperDesign;
	private JasperPrint jasperPrint;

	private String path;

	/** Creates new form PanelLogin */
	public FormMainIDCard() {
		em = new Empl();
		empl = new EmployeeImpl();

		initComponents();
		setOpaque(false);
		setSize(606, 317);

	}

	private void initComponents() {

		lblNama = new JLabel();
		lblNik = new JLabel();

		txtName = new TextBoxGlass();
		txtNik = new TextBoxGlass();

		buttonTransparan1 = new ButtonTransparan();
		buttonTransparan1.setIcon(new javax.swing.ImageIcon(getClass()
				.getResource("/edu/unindra/company/image/operator.png")));

		setBackground(new java.awt.Color(153, 0, 153));
		setOpaque(false);

		lblNama.setFont(new java.awt.Font("Tahoma", 1, 12));
		lblNama.setForeground(Color.BLACK);
		lblNama.setText("Nama");

		lblNik.setFont(new java.awt.Font("Tahoma", 1, 12));
		lblNik.setForeground(Color.BLACK);
		lblNik.setText("Nim");

		lblJabatan = new JLabel();
		lblJabatan.setText("Jabatan");
		lblJabatan.setForeground(Color.BLACK);
		lblJabatan.setFont(new Font("Tahoma", Font.BOLD, 12));

		txtJabatan = new TextBoxGlass();
		txtJabatan.setColumns(10);

		lblTitle = new JLabel("Design IDCard");
		lblTitle.setFont(new Font("Tahoma", Font.BOLD, 14));

		JLabel lblNim = new JLabel("NIM");
		lblNim.setFont(new Font("Tahoma", Font.BOLD, 12));

		txtNim = new TextBoxGlass();
		txtNim.setColumns(10);

		ButtonGlass btnSearch = new ButtonGlass();
		btnSearch.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnSearch.setText("Search");
		btnSearch.setIcon(new ImageIcon(FormMainIDCard.class
				.getResource("/edu/unindra/company/image/magnifier.png")));
		btnSearch.setRoundRect(true);
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnSearchActionPerformed(e);
			}
		});

		btnPrint = new ButtonGlass();
		btnPrint.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnPrint.setIcon(new ImageIcon(FormMainIDCard.class
				.getResource("/edu/unindra/company/image/PRINTER.png")));
		btnPrint.setText("Print");
		btnPrint.setRoundRect(true);
		btnPrint.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnPrintActionPerformed(evt);
			}
		});
		btnPrint.setEnabled(false);

		JLabel lblIdCard = new JLabel("ID Card");
		lblIdCard.setFont(new Font("Tahoma", Font.BOLD, 14));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		layout.setHorizontalGroup(layout
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGroup(
										layout.createParallelGroup(
												Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addGap(5)
																.addComponent(
																		buttonTransparan1,
																		GroupLayout.PREFERRED_SIZE,
																		GroupLayout.DEFAULT_SIZE,
																		GroupLayout.PREFERRED_SIZE)
																.addPreferredGap(
																		ComponentPlacement.RELATED)
																.addGroup(
																		layout.createParallelGroup(
																				Alignment.LEADING)
																				.addGroup(
																						layout.createSequentialGroup()
																								.addGroup(
																										layout.createParallelGroup(
																												Alignment.LEADING)
																												.addComponent(
																														lblNik,
																														GroupLayout.PREFERRED_SIZE,
																														47,
																														GroupLayout.PREFERRED_SIZE)
																												.addComponent(
																														lblNama,
																														GroupLayout.PREFERRED_SIZE,
																														49,
																														GroupLayout.PREFERRED_SIZE)
																												.addComponent(
																														lblJabatan,
																														GroupLayout.PREFERRED_SIZE,
																														59,
																														GroupLayout.PREFERRED_SIZE))
																								.addPreferredGap(
																										ComponentPlacement.RELATED)
																								.addGroup(
																										layout.createParallelGroup(
																												Alignment.TRAILING,
																												false)
																												.addComponent(
																														txtJabatan)
																												.addComponent(
																														txtNik,
																														0,
																														0,
																														Short.MAX_VALUE)
																												.addComponent(
																														txtName,
																														GroupLayout.DEFAULT_SIZE,
																														226,
																														Short.MAX_VALUE)))
																				.addComponent(
																						lblIdCard,
																						GroupLayout.PREFERRED_SIZE,
																						161,
																						GroupLayout.PREFERRED_SIZE)))
												.addGroup(
														layout.createSequentialGroup()
																.addGap(11)
																.addComponent(
																		lblNim,
																		GroupLayout.PREFERRED_SIZE,
																		36,
																		GroupLayout.PREFERRED_SIZE)
																.addPreferredGap(
																		ComponentPlacement.RELATED)
																.addComponent(
																		txtNim,
																		GroupLayout.PREFERRED_SIZE,
																		285,
																		GroupLayout.PREFERRED_SIZE)
																.addGap(18)
																.addComponent(
																		btnSearch,
																		GroupLayout.PREFERRED_SIZE,
																		104,
																		GroupLayout.PREFERRED_SIZE)
																.addPreferredGap(
																		ComponentPlacement.RELATED)
																.addComponent(
																		btnPrint,
																		GroupLayout.PREFERRED_SIZE,
																		GroupLayout.DEFAULT_SIZE,
																		GroupLayout.PREFERRED_SIZE))
												.addGroup(
														layout.createSequentialGroup()
																.addContainerGap()
																.addComponent(
																		lblTitle,
																		GroupLayout.PREFERRED_SIZE,
																		161,
																		GroupLayout.PREFERRED_SIZE)))
								.addContainerGap(29, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(Alignment.TRAILING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap(GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)
								.addComponent(lblTitle,
										GroupLayout.PREFERRED_SIZE, 25,
										GroupLayout.PREFERRED_SIZE)
								.addGap(27)
								.addGroup(
										layout.createParallelGroup(
												Alignment.BASELINE)
												.addComponent(
														lblNim,
														GroupLayout.PREFERRED_SIZE,
														14,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(
														txtNim,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(
														btnSearch,
														GroupLayout.PREFERRED_SIZE,
														23,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(
														btnPrint,
														GroupLayout.PREFERRED_SIZE,
														23,
														GroupLayout.PREFERRED_SIZE))
								.addGap(14)
								.addGroup(
										layout.createParallelGroup(
												Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addGap(4)
																.addComponent(
																		lblIdCard,
																		GroupLayout.PREFERRED_SIZE,
																		25,
																		GroupLayout.PREFERRED_SIZE)
																.addGap(21)
																.addGroup(
																		layout.createParallelGroup(
																				Alignment.LEADING)
																				.addComponent(
																						txtName,
																						GroupLayout.PREFERRED_SIZE,
																						GroupLayout.DEFAULT_SIZE,
																						GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						lblNama))
																.addPreferredGap(
																		ComponentPlacement.UNRELATED)
																.addGroup(
																		layout.createParallelGroup(
																				Alignment.LEADING)
																				.addComponent(
																						txtNik,
																						GroupLayout.PREFERRED_SIZE,
																						GroupLayout.DEFAULT_SIZE,
																						GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						lblNik))
																.addPreferredGap(
																		ComponentPlacement.UNRELATED)
																.addGroup(
																		layout.createParallelGroup(
																				Alignment.BASELINE)
																				.addComponent(
																						txtJabatan,
																						GroupLayout.PREFERRED_SIZE,
																						GroupLayout.DEFAULT_SIZE,
																						GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						lblJabatan,
																						GroupLayout.PREFERRED_SIZE,
																						15,
																						GroupLayout.PREFERRED_SIZE)))
												.addGroup(
														layout.createSequentialGroup()
																.addGap(29)
																.addComponent(
																		buttonTransparan1,
																		GroupLayout.PREFERRED_SIZE,
																		GroupLayout.DEFAULT_SIZE,
																		GroupLayout.PREFERRED_SIZE)))
								.addGap(55)));
		this.setLayout(layout);
	}

	protected void btnSearchActionPerformed(ActionEvent e) {
		try {
			em = empl.getUser(txtNim.getText());
		} catch (SQLException e1) {
			e1.printStackTrace();
		}

		if (em != null) {
			path = em.getPhoto().replace('\\', '/');

			txtName.setText(em.getNama());
			txtNik.setText(String.valueOf(em.getNim()));
			txtJabatan.setText(em.getJabatan());
			buttonTransparan1.setIcon(new javax.swing.ImageIcon(path));

			btnPrint.setEnabled(true);
		} else {
			JOptionPane
					.showMessageDialog(this, "Nim Karyawan tidak di temukan");
			txtName.setText("");
			txtNik.setText("");
			txtJabatan.setText("");
			buttonTransparan1.setIcon(new ImageIcon(FormMainIDCard.class
					.getResource("/edu/unindra/company/image/operator.png")));
		}

		txtNim.setText("");

	}

	@SuppressWarnings({ "rawtypes", "unchecked", "unused", "resource" })
	private void btnPrintActionPerformed(ActionEvent evt) {
		ResultSet resultSet;
		try {
			InputStream inputStream = new FileInputStream(new File(path));
			// InputStream inputStream =
			// this.getClass().getResourceAsStream(path);

			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];

			int n = 0;
			while (-1 != (n = inputStream.read(buffer))) {
				baos.write(buffer, 0, n);
			}

			baos.flush();
			byte[] imageInByte = baos.toByteArray();
			baos.close();

			// convert byte array back to BufferedImage
			InputStream in = new ByteArrayInputStream(imageInByte);
			BufferedImage bImageFromConvert = ImageIO.read(in);

			resultSet = empl.cetakNim(txtNik.getText());
			JRResultSetDataSource resultSetDataSource = new JRResultSetDataSource(
					resultSet);

			Map inputParameters = new HashMap();

			inputParameters.put("logo", bImageFromConvert);
			// File file = new File(getClass().getResource(
			// "/edu/unindra/company/report/IDCard.jrxml").toURI());
			InputStream file = getClass().getResourceAsStream(
					"/edu/unindra/company/report/IDCard.jrxml");

			jasperDesign = JRXmlLoader.load(file);
			jasperReport = JasperCompileManager.compileReport(jasperDesign);

			jasperPrint = JasperFillManager.fillReport(jasperReport,
					inputParameters, resultSetDataSource);
			ReportReview rp = new ReportReview(jasperPrint);
			Dimension screenSize = this.getSize();
			Dimension frameSize = rp.getSize();
			rp.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
